var xmlhttp;
var setId = -1;
var listeningId = -1;
var speakingId = -1;
var readingId = -1;
var writingId  = -1;
var tempType = 0;

function IsNumeric(input)
{
    return (input - 0) == input && input.length > 0;
}

function getXmlHttpObject()
{
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	return xmlhttp;
}

function addListening()
{
	addMachine(1);
}
function addSpeaking()
{
	addMachine(2);
}
function addReading()
{
	addMachine(3);
}
function addWriting()
{
	addMachine(4);
}

function addMachine(type)
{
	tempType = type;
	if(setId==-1){
		addSet();
		return;
	}
	
	getXmlHttpObject();
	var content;
	var title;
	if(type == 1){
		content = document.getElementById("listeningContent").value;
		title = document.getElementById("listeningSummary").value;
	}
	else if(type == 2){
		content = document.getElementById("speakingContent").value;
		title = document.getElementById("speakingSummary").value;
	}
	else if(type == 3){
		content = document.getElementById("readingContent").value;
		title = document.getElementById("readingSummary").value;
	}
	else if(type == 4){
		content = document.getElementById("writingContent").value;
		title = document.getElementById("writingSummary").value;
	}
	var url = "../controllers/addTopic.php";
	params = "content=" + encodeURIComponent(content) + "&type=" + type + "&setid=" + setId + "&title=" + title;
	
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.onreadystatechange = addMachineSuccess;
	xmlhttp.send(params);
}

function addMachineSuccess() {
	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{
			var responseText = xmlhttp.responseText.replace(/\r\n/g,"");
			var buttonDiv;
			if(tempType == 1){
				buttonDiv = document.getElementById("listeningDiv");
				buttonDiv.innerHTML = "<p align='center' style='color: red;'>添加听力完成</p>";
			}
			else if(tempType == 2){
				buttonDiv = document.getElementById("speakingDiv");
				buttonDiv.innerHTML = "<p align='center' style='color: red;'>添加口语完成</p>";
			}
			else if(tempType == 3){
				buttonDiv = document.getElementById("readingDiv");
				buttonDiv.innerHTML = "<p align='center' style='color: red;'>添加阅读完成</p>";
			}
			else if(tempType == 4){
				buttonDiv = document.getElementById("writingDiv");
				buttonDiv.innerHTML = "<p align='center' style='color: red;'>添加写作完成</p>";
			}
		}
	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}

function addSet(){
	getXmlHttpObject();
	var title = document.getElementById("title").value;
	var url = "../controllers/addSet.php";
	var params = "title=" + title ;
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.onreadystatechange = addSetSuccess;
	xmlhttp.send(params);
}

function addSetSuccess() {
	if (xmlhttp.readyState == 4)
		if (xmlhttp.status == 200)
		{
			var responseText = xmlhttp.responseText;
			responseText = responseText.replace(/\s+/g,"");
			if(IsNumeric(responseText)){
				setId = responseText;
				addMachine(tempType);
			}
		}
	    else if (request.status == 404)
	        alert("请求的链接不存在");
	    else
	        alert("错误: 错误代码 " + xmlhttp.status);
}

